package jdbc.three.util.tmp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Tmp {

    //实现增删改
    //insert into stu values(?,?,?)
    public static int update(String sql,Object... args)throws SQLException{
        int rs=0;
        Connection con= DBUtil.getConnection();
        PreparedStatement ps=con.prepareStatement(sql);
        if(args!=null){
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
        }
        rs=ps.executeUpdate();
        DBUtil.close(con,ps,null);
        return rs;
    }

    public static <T> List<T> query(String sql, RowMapper<T> rm, Object... args)throws SQLException{
        List<T>list=new ArrayList<>();
        Connection con= DBUtil.getConnection();
        PreparedStatement ps=con.prepareStatement(sql);
        if(args!=null){
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
        }
        ResultSet rs=ps.executeQuery();
        while (rs.next()){
            T t=rm.mapRow(rs);
            list.add(t);
        }
        DBUtil.close(con,ps,rs);
        return list;
    }

}
